package com.itcast.web;


import com.itcast.constant.MessageConstant;
import com.itcast.entity.Result;
import com.itcast.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import static com.itcast.constant.RedisMessageConstant.*;

@RestController
@RequestMapping("validateCode")
public class ValidateCodeController {
    @Autowired
    JedisPool jedisPool;

    @RequestMapping("send4Login")
    public Result send4Login(String telephone) {
        //调用工具生成验证码
        try {
            String sssss = jedisPool.getResource().get("sssss");

            Integer code = ValidateCodeUtils.generateValidateCode(4);

            //获取电话 需要存入redis 中  添加一个前缀方便登录验证
            String jedisCode = jedisPool.getResource().setex(SENDTYPE_LOGIN + telephone, 60 * 60, String.valueOf(code));
            //调用 阿里云发送短信验证码
            // SMSUtils.sendShortMessage(String.valueOf(code),telephone,"SMS_171858705");
            return Result.success(MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(MessageConstant.SEND_VALIDATECODE_FAIL);
        }


    }

    @RequestMapping("send4Order")
    public Result send4Order(String telephone) {
        //调用工具生成验证码
        try {
            Integer code = ValidateCodeUtils.generateValidateCode(4);
            System.out.println("===="+code);
            //获取电话 需要存入redis 中  添加一个前缀方便登录验证
            String jedisCode = jedisPool.getResource().setex(SENDTYPE_ORDER + telephone, 60 * 60, String.valueOf(code));
            //调用 阿里云发送短信验证码
            // SMSUtils.sendShortMessage(String.valueOf(code),telephone,"SMS_171858705");
            return Result.success(MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(MessageConstant.SEND_VALIDATECODE_FAIL);
        }


    }
}
